﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EShop.BussinessObjects;
using EShop.DataAccess;
using EShop.Common;

namespace EShop.BusinessLogic
{
    public class CBLDiscount
    {
        public static List<Discount> GetDiscounts(OrderMaster orderMaster)
        {
            decimal _sumOrder = 0;
            foreach (OrderDetail orderDetail in orderMaster.OrderDetail)
            {
                _sumOrder += orderDetail.Price * orderDetail.Cnt;
            }
            
            Discount discount_Filter = new Discount();
            discount_Filter.ID = (int)cnstDatabase.DBOperationResult.ValueIsNotEffective;
            discount_Filter.IsActive = true;
            discount_Filter.Company = new Company();
            discount_Filter.Company.ID = orderMaster.Company.ID;
            List<Discount> discountListAll = CDADiscount.Select(discount_Filter, true);

            List<Discount> discountListEfective = new List<Discount>();
            foreach (Discount discount in discountListAll)
            {
                if (discount.ActiveDays.Substring((int)DateTime.Now.DayOfWeek - 1, 1) == "1")
                {
                    if ((discount.ActiveHourFrom.Value.TimeOfDay.TotalSeconds <= DateTime.Now.TimeOfDay.TotalSeconds) &&
                        (discount.ActiveHourTo.Value.TimeOfDay.TotalSeconds >= DateTime.Now.TimeOfDay.TotalSeconds))
                    {
                        if (discount.ForDiscountElementID == cnstDiscountElement.Product)
                        {
                            foreach (OrderDetail orderDetail in orderMaster.OrderDetail)
                            {
                                if ((orderDetail.Product.ID == discount.ForDiscountElementValue) &&
                                    (orderDetail.Cnt >= discount.ForCount))
                                    discountListEfective.Add(discount);
                            }
                        }
                        if (discount.ForDiscountElementID == cnstDiscountElement.Euro)
                        {
                            if (_sumOrder >= discount.ForCount)
                                discountListEfective.Add(discount);
                        }
                    }
                }
            }

            var sameGroups = discountListEfective.GroupBy(d => new { d.GroupingCode, d.ForDiscountElementID, d.ForDiscountElementValue}).
                                    Select(d1 => new { GroupingCode = d1.Key.GroupingCode, ForDiscountElementID = d1.Key.ForDiscountElementID, ForDiscountElementValue = d1.Key.ForDiscountElementValue, GroupingCount = d1.Count(), MaxGroupingCount = d1.Max(d3 => d3.ForCount) }).
                                    Where(d2 => d2.GroupingCount > 1);

            List<Discount> discountListLast = new List<Discount>();
            foreach (Discount discount in discountListEfective)
            {
                bool isAdd = true;
                for (int i = 0; i < sameGroups.Count(); i++)
                {
                    if ((discount.GroupingCode == sameGroups.ElementAt(i).GroupingCode) &&
                        (discount.ForDiscountElementID == sameGroups.ElementAt(i).ForDiscountElementID) &&
                        (discount.ForDiscountElementValue == sameGroups.ElementAt(i).ForDiscountElementValue) &&
                        (discount.ForCount != sameGroups.ElementAt(i).MaxGroupingCount))
                    {
                        isAdd = false;
                        break;
                    }
                }
                if (isAdd)
                    discountListLast.Add(discount);
            }

            return discountListLast;
        }
    }
}
